<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-5-15 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
				
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();

		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			default:
				$this->main();
				break;
		}

		$body = ob_get_clean();

		$this->showBody($body);
	}
	
	private function main()
	{
		//DBFunction::getInstance()->setDebug();
		
		$page = $this->request['offset'] + 1;

		$name = isset($this->request['name']) ? trim($this->request['name']) : '';
		
		$query_link = "manage.php?m=visitor";
		$where = '1';
		if($name)
		{
			$where = " (u.`phone` like '%$name%' or u.`nick_name` like '%$name%')";
			$query_link .= "&name=".urlencode($name);
		}
		
		$sql = " select count(1) as `total_record` from `tra_visitor` u where {$where} ";
		$total_record = DBFunction::getInstance()->getOne($sql);
		
		$item_list = array();
		if($total_record > 0)
		{
			$offset = $page * $this->request['offset'];
			$sql = "select * 
						from `tra_visitor` u 
						where {$where} 
						order by u.`id` asc 
						limit {$offset},{$this->request['limit']} ";
			$item_list = DBFunction::getInstance()->getAll($sql);
		}
		
		$page_html = page($total_record, $this->request['limit'], $page, $query_link, '20 50 100 200 500');
		
		$action = "do_delete";
		include template('admin/visitor_list');
	}

	private function add()
	{
		$request = array();
		if(isset($this->request['c']))
		{
			$request = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$request['nick_name'] = isset($request['nickname']) ? trim($request['nickname']) : '';
		$request['phone'] = isset($request['phone']) ? trim($request['phone']) : '';
		$request['password'] = '';
				
		$action_name = '添加用户';
		$action = "do_add";
		include template('admin/visitor_info');
	}
	
	private function doAdd()
	{
		$request['time'] = time();
		
		$request['nick_name'] = isset($this->request['nickname']) ? trim($this->request['nickname']) : '';
		$request['password'] = isset($this->request['password']) ? trim($this->request['password']) : '';
		$request['phone'] = isset($this->request['phone']) ? trim($this->request['phone']) : '';
		
		$key = md5(implode(',', $request));
		cache_set($key, $request);
		
		$redirect = "manage.php?m=visitor&a=add&c=$key";
		
		if(!$request['nickname'] || !$request['phone'] || !$request['password'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($request['nickname']) > 29)
		{
			$this->messager("用户昵称", $redirect);
		}
		if(strlen($request['phone']) > 29)
		{
			$this->messager("电话超长", $redirect);
		}
		
		$request['password'] = $this->createPassword($request['time'], $request['password']);
		
		$result = add_visitor($request);
		
		if($result)
		{
			$this->messager("添加成功", 'manage.php?m=visitor&a=search');
		}
		else
		{
			$this->messager("添加失败", $redirect);
		}
	}
	
	private function doDelete()
	{		
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		if(check_array($ids))
		{
			$ids = implode(',', $ids);
		}
		
		if(!$ids)
		{
			$this->messager("请指定ID", 'manage.php?m=visitor');
		}
		
		if(stripos($ids, ','))
		{
			$ids = explode(',', $ids);
			
			foreach($ids as $id)
			{
				$result = delete_visitor($id);
			}
		}
		else
		{
			$result = delete_visitor($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'manage.php?m=visitor');
		}
		else
		{
			$this->messager("删除失败", 'manage.php?m=visitor');
		}
	}
	
	private function modify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$visitor_id = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$visitor_id)
		{
			$this->messager("请指定ID", 'manage.php?m=visitor');
		}
		
		if(isset($this->request['c']) && ($request = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$request = get_visitor(array('id'=>$visitor_id));
			
			if(!isset($request['id']) || !$request['id'])
			{
				$this->messager("用户不存在", 'manage.php?m=visitor');
			}
		}
				
		$action = 'do_modify&id='.$request['id'];
		$action_name = '编辑用户';
		include template('admin/visitor_info');
	}
	
	private function doModify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$uid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		$redirect = 'manage.php?m=visitor';
		
		if(!$uid)
		{
			$this->messager("请指定ID", $redirect);
		}
		
		$visitor = get_visitor(array('id'=>$uid));
		
		if(!isset($visitor['id']) || $visitor['id'] < 1)
		{
			$this->messager("用户不存在", $redirect);
		}
		
		//$info = $this->_check_business_info();
		$info['nick_name'] = isset($this->request['nickname']) ? $this->request['nickname'] : '';
		$info['phone'] = isset($this->request['phone']) ? $this->request['phone'] : '';
		$info['password'] = isset($this->request['password']) ? $this->request['password'] : '';
		
		if(!$info['phone'])
        {
			$this->messager("手机号不能为空", 'manage.php?m=visitor&a=modify&id='.$uid);
		}
		if(strlen($info['phone']) > 29)
		{
			$this->messager("电话超长", 'manage.php?m=visitor&a=modify&id='.$uid);
		}
		
		$info['id'] = $uid;
		$info['uid'] = $this->user['id'];
		
		if(!$info['password'])
		{
			$info['password'] = $visitor['password'];
		}
		else
		{
			$info['password'] = $this->createPassword($visitor['create_date'], $info['password']);
		}
		
		$result = update_visitor($info);
		
		if($result)
		{
			$this->messager("修改成功", $redirect);
		}
		else
		{
			$this->messager("修改失败", $redirect);
		}
	}
}